﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class DanhMucDAO
    {
        //Nho Comment Nhung Gi Minh Lam Nhu Vay Nhe ^^
        //Lay danh sach danh muc san pham
        public static DataTable Ds_DanhMuc()
        {
            DataService dservice = new DataService();
            dservice.Connect();
            DataTable dt = dservice.ExcuteQueryDataTable("Select * From DanhMuc ");
            dservice.Disconnect();
            return dt;
        }
        //Lay ten danh muc
        public static string TenDM(int id)
        {
            DataService dservice = new DataService();
            dservice.Connect();
            DataTable dt = dservice.ExcuteQueryDataTable("Select TenDM From DanhMuc Where MaDM="+id);
            dservice.Disconnect();
            return dt.Rows[0]["TenDM"].ToString();
        }
        public static List<DanhMucDTO> LoadDanhMuc()
        {
            DataTable dt = new DataTable();
            string sql = "Select * from DanhMuc";
            dt = Dataprovider.ExcuteQuery(sql);
            List<DanhMucDTO> ls = new List<DanhMucDTO>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DanhMucDTO dm = new DanhMucDTO();
                dm.MaDM = int.Parse(dt.Rows[i]["MaDM"].ToString());
                dm.TenDM = dt.Rows[i]["TenDM"].ToString();
                ls.Add(dm);
            }
            return ls;
        }
        //thêm danh mục
        public static int ThemDanhMuc(DanhMucDTO dm)
        {
            try
            {
                string sql = "insert into danhmuc values(N'" + dm.TenDM + "')";
                return Dataprovider.ExcuteNonQuery(sql);
            }
            catch
            {
                return 0;
            }
        }
        //kiem tra trùng
        public static int KiemTraTrung(string tendm)
        {
            try
            {
                string sql = "select count(*) from danhmuc where tendm like N'" + tendm + "'";
                return int.Parse(Dataprovider.ExcuteScalar(sql));
            }
            catch
            {
                return 0;
            }
        }
    }
}
